 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.rca.dto.in.UserDto;
import com.zdkj.subway.rca.entity.SwCkOthenTicketreturnTmp;
import com.zdkj.subway.rca.service.SwCkOthenTicketreturnService;
import com.zdkj.subway.rca.service.SwCkOthenTicketreturnTmpService;
import com.zdkj.subway.rca.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 报表调账-其他运营商退换票导入表
 *
 * @author stw
 * @date 2020-12-31 19:20:30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckothenticketreturn" )
@Api(value = "swckothenticketreturn", tags = "报表调账-其他运营商退换票导入表管理")
public class SwCkOthenTicketreturnController {


    @Autowired
    private SwCkOthenTicketreturnTmpService swCkOthenTicketreturnTmpService;
    @Autowired
    private SwCkOthenTicketreturnService swCkOthenTicketreturnService;

    /**
     * 导入预处理查询
     * @return
     */
    @ApiOperation(value = "导入预处理查询", notes = "导入预处理查询")
    @GetMapping("/listPreprocessing" )
    //@PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public R listPreprocessing() {
        PigxUser user = SecurityUtils.getUser();
        SwCkOthenTicketreturnTmp swCkOthenTicketreturnTmp = new  SwCkOthenTicketreturnTmp();
        swCkOthenTicketreturnTmp.setCreatorId(user.getId());
        List<SwCkOthenTicketreturnTmp> list = swCkOthenTicketreturnTmpService.list(Wrappers.query(swCkOthenTicketreturnTmp));
		
		//需要修改
        if(list != null && !list.isEmpty()){          
			List<Map<String,String>> projectItemMap = swCkOthenTicketreturnService.selectPaymentMethodItemMap();
			List<Map<String,String>> serviceOrgItemMap = swCkOthenTicketreturnService.selectServiceOrgItemMap();
            for(SwCkOthenTicketreturnTmp obj : list){
                     obj.setSalePaymentmethod(getProjectItemName(projectItemMap,obj.getSalePaymentmethod()));
                     obj.setReturnPaymentmethod(getProjectItemName(projectItemMap,obj.getReturnPaymentmethod()));
					 obj.setServiceOrg(getServiceOrgItemName(serviceOrgItemMap,obj.getServiceOrg()));
                 }
        }
        return R.ok(list);
    }
    /**
     * 获取项目名称
     * @param projectItemMap
     * @param projectItem
     * @return
     */
    private String getProjectItemName (List<Map<String,String>> projectItemMap,String projectItem) {
        if(StringUtils.isNotEmpty(projectItem)){
            for (Map<String,String> map : projectItemMap){
                if(projectItem.equals(map.get("value"))){
                    return map.get("label");
                }
            }
        }

        return "/";
    }
    /**
     * 获取运营商名称
     * @param projectItemMap
     * @param projectItem
     * @return
     */
    private String getServiceOrgItemName (List<Map<String,String>> projectItemMap,String projectItem) {
        if(StringUtils.isNotEmpty(projectItem)){
            for (Map<String,String> map : projectItemMap){
                if(projectItem.equals(map.get("serviceId"))){
                    return map.get("serviceName");
                }
            }
        }
        return "/";
    }
    /**
     * 预处理确认
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "预处理确认", notes = "预处理确认")
    //@SysLog("其他运营商退换票预处理确认")
    @PostMapping({ "/saveBatchOk" })
    //@PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public R saveBatchOk() throws Exception {
        PigxUser pigxUser = SecurityUtils.getUser();
        UserDto user = new UserDto();
        if(pigxUser == null){
            user = UserUtil.userDto();
        }else {
            user = UserUtil.pigxUser();
        }
        this.swCkOthenTicketreturnTmpService.saveBatchWithExcelOk(user);
        return R.ok(true);
    }
    /**
     * 预处理取消 
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "预处理取消", notes = "预处理取消")
    @SysLog("其他运营商退换票预处理取消")
    @PostMapping({ "/saveBatchCancel" })
    //@PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public R saveBatchCancel() throws Exception {
        PigxUser user = SecurityUtils.getUser();
        this.swCkOthenTicketreturnTmpService.saveBatchWithExcelCancel(user.getId());
        return R.ok(true);
    }
}
